package com.fanrui.code.family;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;

import org.apache.commons.lang3.StringUtils;

import com.fanrui.code.entity.FamilyJob;
import com.fanrui.proto.cmd.FamilyJobPower;

public class FamilyJobEntity implements Comparable<FamilyJobEntity>{

	private FamilyJob config;
	private Collection<FamilyJobPower> powers = Collections.emptyList();
	
	public FamilyJobEntity(FamilyJob config) {
		this.config = config;
		init(config.getJobCompetence());
	}
	
	private void init(String powerStr){
		if(StringUtils.isBlank(powerStr)){
			return;
		}
		try {
			String[] powerStrArray = powerStr.split(",");
			powers = new ArrayList<FamilyJobPower>();
			for(String power : powerStrArray){
				powers.add(FamilyJobPower.forNumber(Integer.parseInt(power)));
			}
		} catch (Exception e) {
			throw new IllegalArgumentException("家族职位格式错误:"+powerStr);
		}
	}
	
	public int getJobId(){
		return config.getId();
	}
	
	public boolean checkHavePower(FamilyJobPower power){
		return powers.contains(power);
	}

	@Override
	public int compareTo(FamilyJobEntity o) {
		if(o.getJobId() < getJobId()){
			return -1;
		}
		if(o.getJobId() > getJobId()){
			return 1;
		}
		return 0;
	}
}
